package aerouacmsla.interfazDeUsuario;

import aerouacmsla.entidad.Vuelo;
import aerouacmsla.procedimientos.BDConexion;
import aerouacmsla.procedimientos.CreateTables;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Marco
 */
public class PanelAdministrarVuelos extends javax.swing.JPanel {

    private BDConexion miConexion;
    private CreateTables modeloTabla = new CreateTables();

    public PanelAdministrarVuelos() {
        initComponents();
        if (getMiConexion() != null)
            jTable3.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery("SELECT * FROM vuelo;")));
    }

    public PanelAdministrarVuelos(BDConexion miConexion) {
        initComponents();
        setMiConexion(miConexion);
        jTable3.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery("SELECT * FROM vuelo;")));
    }

    public void setTabla(String SELECT){
        jTable3.setModel(getModeloTabla().getTableModel(getMiConexion().sendSQLQuery(SELECT)));
    }

    public BDConexion getMiConexion() {
        return miConexion;
    }

    public void setMiConexion(BDConexion miConexion) {
        this.miConexion = miConexion;
    }

    public CreateTables getModeloTabla() {
        return modeloTabla;
    }

    public void setModeloTabla(CreateTables modeloTabla) {
        this.modeloTabla = modeloTabla;
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        panelTabla2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new org.jdesktop.swingx.JXTable();
        panelBotones = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jRadioFecha = new javax.swing.JRadioButton();
        jRadioHora = new javax.swing.JRadioButton();
        jRadioAsientos = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        jScrollPane3.setAutoscrolls(true);

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable3.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane3.setViewportView(jTable3);

        javax.swing.GroupLayout panelTabla2Layout = new javax.swing.GroupLayout(panelTabla2);
        panelTabla2.setLayout(panelTabla2Layout);
        panelTabla2Layout.setHorizontalGroup(
            panelTabla2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
        );
        panelTabla2Layout.setVerticalGroup(
            panelTabla2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
        );

        panelBotones.setBorder(javax.swing.BorderFactory.createTitledBorder("Funciones"));

        jButton1.setText("Nuevo Vuelo");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Modificar Vuelo");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setText("Buscar Vuelo");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText("Ver Vuelos");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        buttonGroup1.add(jRadioFecha);
        jRadioFecha.setSelected(true);
        jRadioFecha.setText("Fecha");

        buttonGroup1.add(jRadioHora);
        jRadioHora.setText("Hora");

        buttonGroup1.add(jRadioAsientos);
        jRadioAsientos.setText("Asientos Disponibles");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/IconoVuelos32px.png"))); // NOI18N
        jLabel1.setText("Vuelos");

        javax.swing.GroupLayout panelBotonesLayout = new javax.swing.GroupLayout(panelBotones);
        panelBotones.setLayout(panelBotonesLayout);
        panelBotonesLayout.setHorizontalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelBotonesLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioAsientos)
                            .addComponent(jRadioHora)
                            .addComponent(jRadioFecha)
                            .addGroup(panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(panelBotonesLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        panelBotonesLayout.setVerticalGroup(
            panelBotonesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotonesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(23, 23, 23)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addGap(14, 14, 14)
                .addComponent(jRadioFecha)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioHora)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioAsientos)
                .addContainerGap(41, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelBotones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panelTabla2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(46, 46, 46))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelBotones, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelTabla2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(38, 38, 38))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked
        setTabla("SELECT * FROM vuelo");
    }//GEN-LAST:event_jButton4MouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        VentanaNuevoVuelo nuevoVuelo = new VentanaNuevoVuelo(getMiConexion());
        nuevoVuelo.setVisible(true);
        setTabla("SELECT * FROM vuelo");
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        int fila = jTable3.getSelectedRow();
        if (fila > -1){
            Vuelo modificarVuelo = new Vuelo();
            //Trae el ID del usuario seleccionada
            String id = jTable3.getValueAt(fila, 0).toString();
            String SQL = "SELECT * FROM vuelo WHERE id_vuelo = '" + id + "';";

            getMiConexion().sendSQLQuery(SQL);
            try {
                if (getMiConexion().getResultado().next()) {
                    modificarVuelo.setIdVuelo(id);
                    modificarVuelo.setIdAvion(getMiConexion().getResultado().getObject("id_avion").toString());
                    modificarVuelo.setAsientosDisponibles(Integer.parseInt(getMiConexion().getResultado().getObject("asientos_disponibles").toString()));
                    modificarVuelo.setFechaSalida(getMiConexion().getResultado().getObject("fecha").toString());
                    modificarVuelo.setHoraSalida(getMiConexion().getResultado().getObject("hora_salida").toString());
                    modificarVuelo.setOrigen(getMiConexion().getResultado().getObject("origen").toString());
                    modificarVuelo.setDestino(getMiConexion().getResultado().getObject("destino").toString());
                    modificarVuelo.setPrecio(Float.parseFloat(getMiConexion().getResultado().getObject("precio").toString()));
                    modificarVuelo.setEstado(getMiConexion().getResultado().getObject("estado").toString());

                    VentanaNuevoVuelo modificarUnVuelo = new VentanaNuevoVuelo(modificarVuelo, getMiConexion());
                    modificarUnVuelo.setVisible(true);
                    setTabla("SELECT * FROM vuelo");
                }
            } catch (SQLException ex) {
                Logger.getLogger(PanelAdministrarVuelos.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        String SQL;
        if (jRadioFecha.isSelected())
            SQL = "SELECT * FROM vuelo WHERE fecha ='" + jTextField1.getText() + "'";
        else if (jRadioHora.isSelected())
            SQL = "SELECT * FROM vuelo WHERE hora_salida ='" + jTextField1.getText() + "'";
        else
            SQL = "SELECT * FROM vuelo WHERE asientos_disponibles ='" + jTextField1.getText() + "'";
        setTabla(SQL);
    }//GEN-LAST:event_jButton3MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioAsientos;
    private javax.swing.JRadioButton jRadioFecha;
    private javax.swing.JRadioButton jRadioHora;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel panelBotones;
    private javax.swing.JPanel panelTabla2;
    // End of variables declaration//GEN-END:variables

}
